<?php


namespace App\Services\Admin;


use App\Models\Admin;
use App\Models\Role;
use App\Models\RolePermission;

class RoleService
{
    public function __construct()
    {

    }
    /**
     * 获取所有角色
     * @param $params
     * @return \Illuminate\Http\JsonResponse
     */
    public function getRoleList($params){
        $take = $params['pageSize']?:10;
        $skip = (($params['page']?:1) - 1) * $take;
        $roles = Role::skip($skip)
            ->take($take)
            ->orderBy('order_no','asc')
            ->get();
        foreach ($roles as $role){
            if ($role->role_value == 'super_admin'){
                $menu = PermissionService::allMenuCode();
            }else{
                $menu = PermissionService::menuCodeRole($role->id);
            }
            $role->menu = $menu;
        }
        return Success('ok', $roles);
    }

    /**
     * 获取所有可用角色
     * @return \Illuminate\Http\JsonResponse
     */
    public function getAllRoleList(){
        $roles = Role::where('status',1)->get();
        return Success('ok', $roles);
    }

    /**
     * 设置角色状态
     * @param $params
     * @return \Illuminate\Http\JsonResponse
     */
    public function setRoleStatus($params){
        $role = Role::find($params['id']);
        $role->status = $params['status'];
        if ($role->save()){
            return Success('ok',$role);
        }else{
            return Error();
        }
    }

    /**
     * 更新/添加角色
     * @param $data
     * @return \Illuminate\Http\JsonResponse
     */
    public function createOrUpdateRole($data){
        if ($data['isUpdate']){
            $role = Role::find($data['id']);
            //检查角色名
            if (trim($data['role_name']) !== $role->role_name){
                $exist_role = Role::where('role_name', trim($data['role_name']))
                    ->first();
                if ($exist_role){
                    return Error('该角色名已存在！');
                }
            }

            //检测角色值
            if (trim($data['role_value']) !== $role->role_value) {
                $exist_role_value = Role::where('role_value', trim($data['role_value']))
                    ->first();
                if ($exist_role_value) {
                    return Error('该角色指已存在！');
                }
            }
        }else{
            $role = new Role();

            //检查角色名
            $exist_role = Role::where('role_name', trim($data['role_name']))
                ->first();
            if ($exist_role){
                return Error('该角色名已存在！');
            }

            //检测角色值
            $exist_role_value = Role::where('role_value', trim($data['role_value']))
                ->first();
            if ($exist_role_value) {
                return Error('该角色指已存在！');
            }
        }

        $role->role_name = trim($data['role_name']);
        $role->role_value = trim($data['role_value']);
        $role->remark = trim($data['remark']);
        $role->order_no = trim($data['order_no']);
        $role->status = trim($data['status']);

        if ($role->save()){
            //同步关联
            $role->permission()->sync($data['menu']);
            return Success('操作成功！');
        }else{
            return Success('操作失败！');
        }
    }

    /**
     * 删除角色
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteRole($id){
        $roleUser = Admin::where('role_id',$id)
            ->first();
        if ($roleUser){
            return Error('操作失败，该角色下含有已分配的管理员，不可删除！',$id);
        }

        //同时删除角色对应菜单
        if (Role::destroy($id)){
            $deleted = RolePermission::where('role_id',$id)->delete();
            if ($deleted){
                return Success('删除成功');
            }else{
                return Error('删除失败！');
            }
        }else{
            return Error('删除失败！');
        }
    }
}
